<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class UserdetailsController extends Controller
{ 
    //添加页面 接收要修改的id号
   public function getAdd($id=0){
    
    //用户表的id 等于用户详情表的uid
    $uid = $id;
    
    //返回添加页面
    return view('admin/userdetails/add',['uid' => $uid]);
   }


   public function postInsert(Request $request){

        //接收form表单的值 并进行验证 
        $this -> validate($request,[
            'truename' => 'required',
            'sex' => 'required',
            'idcard' => 'required|unique:shop_userdetails,idcard|digits:18',
            'adress' => 'required'
            ],[
            'truename.required' => '用户名必须填写',
            'sex.required' => '性别必须填写',
            'idcard.required' => '身份证号必须填写',
            'idcard.unique' => '身份证号已存在',
            'idcard.digits' => '身份证号要18位',
            'adress.required' => '收货地址必须填写'
            ]);

        //去除不需要存入的值
        $arr = $request -> except(['_token']);
        
        //插入数据
        $res = DB::table('shop_userdetails') -> insert($arr);

        //进行判断 如果添加成功跳转至index页面 如果失败,利用事件回滚返回
        if($res){
            return redirect('admin/userdetails/index') -> with('success','添加成功');
        }else{
            return back() -> with('error','添加失败');
        }
    }

    //查询所有的数据
    public function getIndex(Request $request){

        //查询数据 并且分页
        $count = $request -> input('count',10);
        $search = $request -> input('search','');

        //让用户详情表的uid进行升序排序
<<<<<<< HEAD
        $data = DB::table('shop_userdetails') -> orderBy('uid','asc')
        -> join('shop_user','shop_user.id','=','shop_userdetails.uid')
        -> where('truename','like','%'.$search.'%') -> paginate($count);
        
        //返回index页面
        return view('admin/userdetails/index',['data' => $data,'request' => $request ->all()]);
=======
        $data = DB::table('shop_userdetails') 
        -> join('shop_user','shop_user.id','=','shop_userdetails.uid')
        -> where('truename','like','%'.$search.'%')-> orderBy('uid','asc') -> paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        /*$sum = count(DB :: table('shop_userdetails') 
        ->join('shop_user','shop_user.id','=','shop_userdetails.uid') 
        ->where('truename','like','%'.$search.'%')->get()); */
        $sum = count(DB :: table('shop_userdetails') ->join('shop_user', 'shop_user.id','=','shop_userdetails.uid') ->where('truename','like','%'.$search.'%')->get()); 
        $end = min($page * $count,$sum);
        //返回index页面

        return view('admin/userdetails/index',['data'=>$data,'request'=>$request -> all(), 'start' => $start, 'end' => $end, 'sum' => $sum]);
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    }

      //获取删除数据的id号
     public function getDelete($id){

            //删除用户详情表中的数据
            $res = DB::table('shop_userdetails') -> where('uid',$id) -> delete();

            //进行判断 如果添加成功跳转至index页面 如果失败,利用事件回滚返回
            if($res){
                return redirect('/admin/userdetails/index') -> with('success','删除成功');
            }else{
                return back() -> with('error','删除失败');
            }
        }

    //接收用户表的id 进行修改
    public function getEdit($id){

        //通过用户的id号进行查询 并且返回用户所有信息
        $data = DB::table('shop_userdetails') 
        -> join('shop_user','shop_user.id','=','shop_userdetails.uid')
        -> where('uid',$id) 
        -> first();
        
        //返回修改页面
        return view('admin/userdetails/edit',['data' => $data]);
    }

    //修改
    public function postUpdate(Request $request){
       
            //先进行判断 验证
           $this -> validate($request,[
              'truename' => 'required',
              'sex' => 'required',
              'idcard' => 'required|digits:18',
              'adress' => 'required'
            ],[
              'truename.required' => '用户名必须填写',
              'sex.required' => '性别必须填写',
              'idcard.required' => '身份证号必须填写',
              'idcard.digits' => '身份证号要18位',
              'adress.required' => '收货地址必须填写'
            ]);
        
            //去除不需要修改的数据
            $arr = $request -> except(['_token','id','uid']);
            
            //把数据添加到数据表中
            $res = DB::table('shop_userdetails') -> where('uid',$request->input('uid')) -> update($arr);
            
             //判断 如果成功 如果添加成功跳转至index页面 如果失败,利用事件回滚返回
            if($res){
                return redirect('/admin/userdetails/index') -> with('success','修改成功');
            }else{
                return back() -> with('error','修改失败');
            }
    }
}
